﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;

namespace DroidClaim.BL.Utility
{
    public class ImageString
    {
        // Image i = Image.FromFile("image.jpg");
        // Image i = Image.FromFile(openFileDialog1.Filename);
        public static string ImageToBase64(Image image, ImageFormat format)
        {
            if (image == null)
            {
                return "";
            }

            using (MemoryStream ms = new MemoryStream())
            {
                // Convert Image to byte[]
                image.Save(ms, format);
                byte[] imageBytes = ms.ToArray();

                // Convert byte[] to Base64 String
                string base64String = Convert.ToBase64String(imageBytes);
                return base64String;
            }
        }

        public static Image Base64ToImage(string base64String)
        {
            if (base64String == null || base64String == "")
            {
                return null;
            }

            // Convert Base64 String to byte[]
            byte[] imageBytes = Convert.FromBase64String(base64String);
            MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);

            // Convert byte[] to Image
            ms.Write(imageBytes, 0, imageBytes.Length);
            Image image = Image.FromStream(ms, true);

            return image;
        }

        /*public string ImageStringToFile(String imageStr)
        {
            Image image = Base64ToImage(imageStr);
        }*/
    }
}